/**
 * 
 */
package sob.me.threadapp.demo1;

import java.util.List;

/**
 * @author blackcore
 * @see 上午10:29:39
 * 
 */
public class Pager<T> {

	private int page = 1; // 当前页

	public int totalPages = 0; // 总页数

	private int pageSize;// 每页5条数据

	private int totalRows = 0; // 总数据数

	private int pageStartRow = 0;// 每页的起始数

	private int pageEndRow = 0; // 每页显示数据的终止数

	private boolean hasNextPage = false; // 是否有下一页

	private boolean hasPreviousPage = false; // 是否有前一页

	private List<T> list;

	public Pager(List<T> list, int pageSize) {
		init(list, pageSize);// 通过对象集，记录总数划分
	}

	/** */
	/**
	 * 初始化list，并告之该list每页的记录数
	 * 
	 * @param list
	 * @param pageSize
	 */
	public void init(List<T> list, int pageSize) {
		this.pageSize = pageSize;
		this.list = list;
		totalRows = list.size();
		hasPreviousPage = false;
		if ((totalRows % pageSize) == 0) {
			totalPages = totalRows / pageSize;
		} else {
			totalPages = totalRows / pageSize + 1;
		}

		if (page >= totalPages) {
			hasNextPage = false;
		} else {
			hasNextPage = true;
		}

		if (totalRows < pageSize) {
			this.pageStartRow = 0;
			this.pageEndRow = totalRows;
		} else {
			this.pageStartRow = 0;
			this.pageEndRow = pageSize;
		}
	}

	// 判断要不要分页
	public boolean isNext() {
		return list.size() > 5;
	}

	public void setHasPreviousPage(boolean hasPreviousPage) {
		this.hasPreviousPage = hasPreviousPage;
	}

	public String toString(int temp) {
		String str = Integer.toString(temp);
		return str;
	}

	public void description() {

		StringBuilder description = new StringBuilder();
		description.append("共有数据数:");
		description.append(this.getTotalRows());
		description.append("\t共有页数: ");
		description.append(this.getTotalPages());
		description.append("\t当前页数为:");
		description.append(this.getPage());
		description.append("\t是否有前一页: ");
		description.append(this.isHasPreviousPage());
		description.append("\t是否有下一页:");
		description.append(this.isHasNextPage());
		description.append("\t开始行数:");
		description.append(this.getPageStartRow());
		description.append("\t终止行数:");
		description.append(this.getPageEndRow());
		System.out.println(description);
	}

	public List<T> getNextPage() {
		page = page + 1;
		disposePage();
		System.out.println("use invoke page of" + page + ".");
		this.description();
		return getObjects(page);
	}

	/** */
	/**
	 * 处理分页
	 */
	private void disposePage() {
		if (page == 0) {
			page = 1;
		}
		if ((page - 1) > 0) {
			hasPreviousPage = true;
		} else {
			hasPreviousPage = false;
		}

		if (page >= totalPages) {
			hasNextPage = false;
		} else {
			hasNextPage = true;
		}
	}

	public List<T> getPreviousPage() {

		page = page - 1;

		if ((page - 1) > 0) {
			hasPreviousPage = true;
		} else {
			hasPreviousPage = false;
		}
		if (page >= totalPages) {
			hasNextPage = false;
		} else {
			hasNextPage = true;
		}
		this.description();
		return getObjects(page);
	}

	/** */
	/**
	 * 获取第几页的内容
	 * 
	 * @param page
	 * @return
	 */
	public List<T> getObjects(int page) {
		if (page == 0)
			this.setPage(1);
		else
			this.setPage(page);
		this.disposePage();
		if (page * pageSize < totalRows) {// 判断是否为最后一页
			pageEndRow = page * pageSize;
			pageStartRow = pageEndRow - pageSize;
		} else {
			pageEndRow = totalRows;
			pageStartRow = pageSize * (totalPages - 1);
		}

		List<T> objects = null;
		if (!list.isEmpty()) {
			objects = list.subList(pageStartRow, pageEndRow);
		}

		// List<String> results = new ArrayList<String>();
		// if(objects != null && objects.size()>0)
		// {
		// for(String item : objects){
		// results.add(item);
		// }
		// }
		// return results;

		// //this.description();

		return objects;

	}

	public List<T> getFistPage() {
		if (this.isNext()) {
			return list.subList(0, pageSize);
		} else {
			return list;
		}
	}

	public boolean isHasNextPage() {
		return hasNextPage;
	}

	public void setHasNextPage(boolean hasNextPage) {
		this.hasNextPage = hasNextPage;
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageEndRow() {
		return pageEndRow;
	}

	public void setPageEndRow(int pageEndRow) {
		this.pageEndRow = pageEndRow;
	}

	public int getpageSize() {
		return pageSize;
	}

	public void setpageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageStartRow() {
		return pageStartRow;
	}

	public void setPageStartRow(int pageStartRow) {
		this.pageStartRow = pageStartRow;
	}

	public int getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}

	public int getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}

	public boolean isHasPreviousPage() {
		return hasPreviousPage;
	}

}
